package com.iflytek.jcy.supervise.clue.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.iflytek.jcy.supervise.clue.common.enums.ResultEnum;
import com.iflytek.jcy.supervise.clue.common.exception.BaseException;
import com.iflytek.jcy.supervise.clue.common.pojo.CommonResponse;
import com.iflytek.jcy.supervise.clue.dto.response.ClueCollectionDTO;
import com.iflytek.jcy.supervise.clue.entity.ClueCollectionMap;
import com.iflytek.jcy.supervise.clue.entity.TaskDetail;
import com.iflytek.jcy.supervise.clue.mapper.ClueCollectionMapMapper;
import com.iflytek.jcy.supervise.clue.mapper.ClueCollectionMapper;
import com.iflytek.jcy.supervise.clue.mapper.TaskDetailMapper;
import com.iflytek.jcy.supervise.clue.service.IClueCollectionMapService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 线索集表 服务实现类
 * </p>
 *
 * @author dgyu
 * @since 2022-10-20 */
@Service
public class ClueCollectionMapServiceImpl extends ServiceImpl<ClueCollectionMapMapper, ClueCollectionMap> implements IClueCollectionMapService {

	private static final Logger	 logger	= LoggerFactory.getLogger(ClueCollectionMapServiceImpl.class);

	@Autowired
	private ClueCollectionMapper clueCollectionMapper;

	@Autowired
	private ClueCollectionMapMapper clueCollectionMapMapper;

	@Autowired
	private TaskDetailMapper	 taskDetailMapper;
	@Override
	public int deleteByDetailId(Integer id) {
		LambdaQueryWrapper<ClueCollectionMap> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(ClueCollectionMap::getTaskDetailId, id);
		return this.baseMapper.delete(lambdaQueryWrapper);
	}

	@Override
	public CommonResponse addClueCollectionMap(JSONObject jSONObject) {
		Integer taskDetailId = jSONObject.getInteger("id");
		TaskDetail taskDetail = this.taskDetailMapper.selectById(taskDetailId);
		if (null == taskDetail) {
			logger.error("线索不存在taskDetailId:{}", taskDetailId);
			throw new BaseException(ResultEnum.EXCEPTION, "线索不存在taskDetailId:{}" + taskDetailId);
		}
		String rybm = jSONObject.getString("rybm");
		if (StringUtils.isEmpty(rybm)) {
			return CommonResponse.failed(ResultEnum.RYBM_IS_NULL.getDesc());
		}
		// 查询已经在线索中线索集id集合
		List<ClueCollectionDTO> list1 = clueCollectionMapper.getCollectionByClueResultId(taskDetailId, rybm, taskDetail.getTaskId());
		//已经加入过的线索集，把时间更新了
		list1.forEach(collection -> {
			LambdaUpdateWrapper<ClueCollectionMap> updateWrapper = new LambdaUpdateWrapper<ClueCollectionMap>()
					.set(ClueCollectionMap::getUpdateTime, new Date())
					.eq(ClueCollectionMap::getTaskDetailId, taskDetailId)
					.eq(ClueCollectionMap::getCollectionId, collection.getId());
			clueCollectionMapMapper.update(null, updateWrapper);
		});
		JSONArray collectionIds = jSONObject.getJSONArray("collectionIds");
		if (null == collectionIds || collectionIds.size() <= 0) {
			return CommonResponse.failed(ResultEnum.CLUE_COLLECTION_UNSELECTED_ERROR.getDesc());
		}
		if (null != collectionIds && collectionIds.size() > 0) {
			List<Integer> list = JSONArray.parseArray(JSON.toJSONString(collectionIds), Integer.class);
			// 过滤出没有加入过的线索集id
			Set<Integer> joinedSet = list1.stream().map(ClueCollectionDTO::getId).collect(Collectors.toSet());
			list = list.stream().filter(v -> !joinedSet.contains(v)).collect(Collectors.toList());
			if (CollectionUtils.isEmpty(list)) {
				return CommonResponse.success();
			}
			List<ClueCollectionMap> maps = new ArrayList<>();
			list.forEach(v -> {
				ClueCollectionMap map = new ClueCollectionMap();
				map.setTaskDetailId(taskDetailId);
				map.setCollectionId(v);
				maps.add(map);
			});
			this.saveBatch(maps);
		}
		return CommonResponse.success();
	}

}
